package it.polito.connectedcar.network;

import java.util.Random;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class DelayedMessageQueue implements MessageQueue{

	private ScheduledExecutorService executorService;
	private final Random delayRandom = new Random(System.currentTimeMillis());
	public DelayedMessageQueue() {
		executorService = Executors.newSingleThreadScheduledExecutor();
	}

	@Override
	public void addMessage(Message message) {
		executorService.schedule(new Runnable() {
			
			@Override
			public void run() {
				/*send the Message to the underlying network service in order to be transmitted*/
				
			}
		}, getSimulatedDelay(), TimeUnit.MILLISECONDS);
		
	}

	/**
	 * This method must simulate the delay in a cellular network for packet transmission
	 * @return
	 */
	private long getSimulatedDelay() {
		
		return delayRandom.nextLong();
	}
	
}
